/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       billboard.cpp
 * Author:     twoja stara
 * Created on: 2009-09-07
 *
 **********************************************************************************************************************/

#include "billboard.h"

namespace mGameEngine
{

namespace Graphics
{

Billboard::Billboard() :
    Drawable(),
    _texture(NULL), _size(), _center(), _orientation()
{
}

Billboard::Billboard(const Texture *texture, const Vector2 &size, const Vector2 &center, float orientation) :
    Drawable(),
    _texture(texture), _size(size), _center(center), _orientation(orientation)
{
    Vector2 r = 0.5f * size;
    _boundingSphere.setRadius(length(r));
    _boundingSphere.setCenter(r);
}

Billboard::~Billboard()
{
}

void Billboard::draw() const
{
    // no texture specified
    if(!_texture)
    {
        return;
    }

    // load texture
    TexturedMaterial mat(_texture);
    mat.set();

    // set matrix orientation
    Matrix4x4 modelview;
    glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
    modelview = Matrix3x3(Quaternion(_orientation, 0.0f, 0.0f));
    modelview.m[3][0] -= _center.x;
    modelview.m[3][1] -= _center.y;
    glLoadMatrixf(modelview);

    // draw billboard
    glBegin(GL_TRIANGLE_STRIP);
        glNormal3fv(Vector3(0.0f, 0.0f, 1.0f));
        glSecondaryColor3fv(Vector3(1.0f, 0.0f, 0.0f));

        glTexCoord2fv(Vector2(0.0f, 0.0f));
        glVertex2fv(Vector2(0.0f, 0.0f));

        glTexCoord2fv(Vector2(1.0f, 0.0f));
        glVertex2fv(Vector2(_size.x, 0.0f));

        glTexCoord2fv(Vector2(0.0f, 1.0f));
        glVertex2fv(Vector2(0.0f, _size.y));

        glTexCoord2fv(Vector2(1.0f, 1.0f));
        glVertex2fv(Vector2(_size.x, _size.y));
    glEnd();
}

}
}

